# -*- coding: utf-8 -*-

import os
from random import randint

import re

_THUMBNAIL_POSTFIX = "_thumb"

def get_thumbnail_path( name, postfix = "" ):
	filename = os.path.basename( name )
	comma_index = filename.rfind( '.' )
	if comma_index != -1:
		filename = filename[ 0: comma_index ] + "{0}{1}.".format( _THUMBNAIL_POSTFIX, postfix ) + filename[ comma_index + 1: len( filename ) ] 
	else:
		filename = filename[ 0: len( filename ) ] + "{0}{1}".format( _THUMBNAIL_POSTFIX, postfix )
	thumbnail_name = os.path.dirname( name ) + "/" + filename	
	return thumbnail_name

def get_small_thumbnail_path( name ):
	return get_thumbnail_path( name, "_s" )

def _get_path_by_index( i, path ):
		filename = os.path.basename( path )
		comma_index = filename.rfind( u'.' )
		if comma_index != -1:
			filename = filename[ 0: comma_index ] + u"_{0}.".format( i ) + filename[ comma_index + 1: len( filename ) ] 
		else:
			filename = filename[ 0: len( filename ) ] + u"_{0}".format( i )
		path = os.path.dirname( path ) + u"/" + filename
		return path

def while_path_exists( path ):
	i = randint( 1, 1000 )
	path_ = _get_path_by_index( i, path )
	filename = os.path.basename( path_ )
	while os.path.exists( path_ ):
		path_ = _get_path_by_index( i, path_ )
		filename = os.path.basename( path_ )
	return filename

def remove_extra_gaps( s ):
	return re.sub( r"(\s+)", " ", s ).strip()

# language-specific functions



class WrongLetterPositionForSlugError( Exception ): pass

def _ru_is_vowel( ch ):
	return ( u'уоыаэиэ'.find( ch ) != -1 )

def _ru_lett1( s, index ):
	"ъ"
	if index == 0:
		raise WrongLetterPositionForSlug
	elif index == len( s ):
		result = ""
	else:
		next = s[ index + 1 ]
		if _ru_is_vowel( next ):
			result = "j"
		else:
			result = ""
	return result

		

_SLAG_MAP_RU = \
{
	u'-': "-",
	u'й': "y",
	u'ц': "c",
	u'у': 'u',
	u'к': "k",
	u'е': "e",
	u'н': "n",
	u'г': "g",
	u'ш': "sh",
	u'щ': "sch",
	u'з': "z",
	u'х': "h",
	u'ъ': _ru_lett1,
	u'ф': "f",
	u'ы': "y",
	u'в': "v",
	u'а': "a",
	u'п': "p",
	u'р': "r",
	u'о': "o",
	u'л': "l",
	u'д': "d",
	u'ж': "j",
	u'э': "e",
	u'я': "ja",
	u'ч': "ch",
	u'с': "s",
	u'м': "m",
	u'и': "i",
	u'т': "t",
	u'ь': _ru_lett1,
	u'б': "b",
	u'ю': "ju",
	u'ё': "jo"
}

def slugify_ru( s ):
	# input: string with extra gaps removed
	result = u""
	i = 0

	for ch in s:		
		if ch == ' ':
			result += '-'
		else:
			ch2 = ch.lower()
			subst = _SLAG_MAP_RU[ ch2 ]
			
			if hasattr( subst, '__call__' ):
				result += subst( s, i ) 
			else:
				result += subst
		i += 1

	return result

if __name__ == '__main__':
	print slugify_ru( u"Лейла Соколова" )
	print slugify_ru( u"Екатерина Абрамова" )
	print slugify_ru( u"Почтальон" )
	print slugify_ru( u"Подъём" )

# detects if the string is in Russian
RUSSIAN_CHARSET = u'йцукенгшщзхъфывапролджэячсмитьбюёЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮЁ'
def _is_russian( s ):
	result = False
	for a in RUSSIAN_CHARSET:
		if s.find( a ) != -1:
			result = True
			break
	return result

def get_face_request( s ):
	if _is_russian( s ):
		result = u"{0} лицо".format( s )
	else:
		result = u"face of {0}".format( s )	
	return result

def get_body_request( s ):
	if _is_russian( s ):
		result = u"{0} тело".format( s )
	else:
		result = u"body of {0}".format( s )		
	return result

def get_nude_request( s ):
	if _is_russian( s ):
		result = u"голая {0}".format( s )
	else:
		result = u"nude {0}".format( s )
	return result